import 'package:flutter/foundation.dart';
import 'package:flutter/material.dart';

import '../widgets/widgets.dart';

part 'theme_light.dart';

part 'theme_dark.dart';

final kVanLightTheme = VanThemeData.fromJson(Brightness.light, _kVanLightThemeData);
final kVanDarkTheme = VanThemeData.fromJson(Brightness.dark, _kVanDarkThemeData);

class VanTheme extends StatelessWidget {
  final VanThemeData theme;
  final Widget child;

  const VanTheme({super.key, required this.theme, required this.child});

  static VanThemeData of(BuildContext context) {
    var theme = VanProvider.of<VanThemeData>(context);
    if (theme != null) return theme;
    final brightness = Theme.of(context).brightness;
    theme = brightness == Brightness.dark ? kVanDarkTheme : kVanLightTheme;
    return theme;
  }

  static get dark => kVanDarkTheme;

  static get light => kVanLightTheme;

  static const _prefix = "--van-";

  static bool support(VanCssKey vanCssKey) => vanCssKey.key.startsWith(_prefix);

  @override
  Widget build(BuildContext context) => VanProvider<VanThemeData>(provide: theme, child: child);
}

@immutable
class VanThemeData with VanProvide {
  final Brightness brightness;
  final VanRootThemeData rootTheme;
  final VanButtonThemeData buttonTheme;
  final VanCellGroupThemeData cellGroupTheme;
  final VanCellThemeData cellTheme;
  final VanImageThemeData imageTheme;
  final VanPopupThemeData popupTheme;

  final VanCheckboxThemeData checkboxTheme;
  final VanFieldThemeData fieldTheme;
  final VanRadioThemeData radioTheme;
  final VanSwitchThemeData switchTheme;

  final VanTagThemeData tagTheme;

  const VanThemeData.raw({
    required this.brightness,
    required this.rootTheme,
    required this.buttonTheme,
    required this.cellGroupTheme,
    required this.cellTheme,
    required this.imageTheme,
    required this.popupTheme,
    //
    required this.checkboxTheme,
    required this.fieldTheme,
    required this.radioTheme,
    required this.switchTheme,
    //
    required this.tagTheme,
  });

  /// 拷贝
  VanThemeData copyFrom({
    Brightness? brightness,
    VanRootThemeData? rootTheme,
    VanButtonThemeData? buttonTheme,
    VanCellThemeData? cellTheme,
    VanCellGroupThemeData? cellGroupTheme,
    VanImageThemeData? imageTheme,
    VanPopupThemeData? popupTheme,

    //
    VanCheckboxThemeData? checkboxTheme,
    VanFieldThemeData? fieldTheme,
    VanRadioThemeData? radioTheme,
    VanSwitchThemeData? switchTheme,

    //
    VanTagThemeData? tagTheme,
  }) {
    return VanThemeData.raw(
      brightness: brightness ?? this.brightness,
      rootTheme: rootTheme ?? this.rootTheme,
      buttonTheme: buttonTheme ?? this.buttonTheme,
      cellTheme: cellTheme ?? this.cellTheme,
      cellGroupTheme: cellGroupTheme ?? this.cellGroupTheme,
      imageTheme: imageTheme ?? this.imageTheme,
      popupTheme: popupTheme ?? this.popupTheme,
      //
      checkboxTheme: checkboxTheme ?? this.checkboxTheme,
      fieldTheme: fieldTheme ?? this.fieldTheme,
      radioTheme: radioTheme ?? this.radioTheme,
      switchTheme: switchTheme ?? this.switchTheme,
      //
      tagTheme: tagTheme ?? this.tagTheme,
    );
  }

  /// 反序列化
  static VanThemeData fromJson(Brightness brightness, Map<String, dynamic> json) {
    return VanThemeData.raw(
      brightness: brightness,
      rootTheme: VanRootThemeData.fromJson(json),
      buttonTheme: VanButtonThemeData.fromJson(json),
      cellTheme: VanCellThemeData.fromJson(json),
      cellGroupTheme: VanCellGroupThemeData.fromJson(json),
      imageTheme: VanImageThemeData.fromJson(json),
      popupTheme: VanPopupThemeData.fromJson(json),
      //
      checkboxTheme: VanCheckboxThemeData.fromJson(json),
      fieldTheme: VanFieldThemeData.fromJson(json),
      radioTheme: VanRadioThemeData.fromJson(json),
      switchTheme: VanSwitchThemeData.fromJson(json),
      //
      tagTheme: VanTagThemeData.fromJson(json),
    );
  }
}
